use crate::entity::{
    prelude::StockTradeJob,
    stock_trade_job::{self, Model},
};

use sea_orm::{prelude::*, *};

pub async fn all(db: &DbConn) -> Result<Vec<Model>, DbErr> {
    StockTradeJob::find().all(db).await
}

pub async fn add(db: &DbConn, stock_code: &str) -> Result<bool, String> {
    let res = StockTradeJob::find_by_id(stock_code).one(db).await;
    if let Ok(Some(_)) = res {
        return Ok(true);
    }
    let model = stock_trade_job::ActiveModel {
        stock_code: Set(stock_code.to_owned()),
    };
    let res = StockTradeJob::insert(model).exec(db).await;
    if res.is_err() {
        return Err("添加失败".to_owned());
    }
    Ok(true)
}

pub async fn delete(db: &DbConn, stock_code: &str) -> Result<DeleteResult, DbErr> {
    StockTradeJob::delete_by_id(stock_code).exec(db).await
}
